package com.wxq.yaoxia;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.wxq.yaoxia.util.YXDBHelper;

public abstract class YXBaseContentProvider extends ContentProvider{
	protected static final String tag = "YXBaseContentProvider";
	
	protected static final int SEARCH_SUGGEST = 0;
	protected static final int SHORTCUT_REFRESH = 1;
	protected UriMatcher sURIMatcher = buildUriMatcher();
	protected YXDBHelper dbHelper;
	
	protected abstract String getAuthority();
	
	protected UriMatcher buildUriMatcher() {
		UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
		
		matcher.addURI(getAuthority(), SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
		matcher.addURI(getAuthority(), SearchManager.SUGGEST_URI_PATH_QUERY+"/*", SEARCH_SUGGEST);
		
		matcher.addURI(getAuthority(), SearchManager.SUGGEST_URI_PATH_SHORTCUT, SHORTCUT_REFRESH);
		matcher.addURI(getAuthority(), SearchManager.SUGGEST_URI_PATH_SHORTCUT+"/*", SHORTCUT_REFRESH);
		
		return matcher;
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		Log.d(tag, "onCreate called");
		dbHelper = new YXDBHelper(this.getContext());
		return true;
	}

	
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder)
	{
		{
			String query = selectionArgs[0];//查询的真正参数！！！
			Log.d(tag, "query="+query);
			
			switch (sURIMatcher.match(uri)) {
			case SEARCH_SUGGEST:
				Log.d(tag, "search suggest called,list show now!");
				//返回对应列名为COLUMNS_STRINGS中字符串的一组值，多行
				return getSuggestCursor(query);
				
			case SHORTCUT_REFRESH:
				Log.d(tag, "shortcut refresh called");
				return null;

			default:
				throw new IllegalArgumentException("Unknown URL "+uri);
			}
		}
	}
	
	public abstract Cursor getSuggestCursor(String query);

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		switch (sURIMatcher.match(uri)) {
		case SEARCH_SUGGEST:
			return SearchManager.SUGGEST_MIME_TYPE;
		case SHORTCUT_REFRESH:
			return SearchManager.SHORTCUT_MIME_TYPE;	

		default:
			throw new IllegalArgumentException("Unknown URL "+uri);
		}
	}
	

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		
		throw new UnsupportedOperationException();
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		
		throw new UnsupportedOperationException();
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		
		throw new UnsupportedOperationException();
	}
}
